package com.beemans.common.utils

import com.beemans.common.app.CommonApp
import com.scwang.smart.refresh.layout.SmartRefreshLayout

/**
 * @author tiamosu
 * @date 2021/3/13
 */
class RefreshUtils(private val smartRefreshLayout: SmartRefreshLayout) {

    companion object {

        fun builder(smartRefreshLayout: SmartRefreshLayout): Builder {
            return Builder(smartRefreshLayout)
        }
    }

    class Builder internal constructor(private val smartRefreshLayout: SmartRefreshLayout) {

        init {
            setEnableLoadMore(false)
            setEnableOverScrollBounce(false)
            setEnableOverScrollDrag(false)
        }

        /**
         * @param enableOverScrollBounce 设置是否启用越界回弹（默认启用）
         */
        fun setEnableOverScrollBounce(enableOverScrollBounce: Boolean): Builder {
            smartRefreshLayout.setEnableOverScrollBounce(enableOverScrollBounce)
            return this
        }

        /**
         * @param enableOverScrollDrag 设置是否启用越界拖动（仿苹果效果）
         */
        fun setEnableOverScrollDrag(enableOverScrollDrag: Boolean): Builder {
            smartRefreshLayout.setEnableOverScrollDrag(enableOverScrollDrag)
            return this
        }

        /**
         * @param enableRefresh 是否开启下拉刷新功能（默认true）
         */
        fun setEnableRefresh(enableRefresh: Boolean): Builder {
            smartRefreshLayout.setEnableRefresh(enableRefresh)
            return this
        }

        /**
         * @param enableLoadMore 是否开启加上拉加载功能（默认false-智能开启）
         */
        fun setEnableLoadMore(enableLoadMore: Boolean): Builder {
            smartRefreshLayout.setEnableLoadMore(enableLoadMore)
            return this
        }

        /**
         * @param enableAutoLoadMore 是否监听列表惯性滚动到底部时触发加载事件（默认true）
         */
        fun setEnableAutoLoadMore(enableAutoLoadMore: Boolean): Builder {
            smartRefreshLayout.setEnableAutoLoadMore(enableAutoLoadMore)
            return this
        }

        fun build(callback: RefreshCallback.() -> Unit = {}): RefreshUtils {
            val refreshCallback = RefreshCallback().apply(callback)
            smartRefreshLayout.setOnRefreshListener { refreshLayout ->
                CommonApp.globalHandler.postDelayed({
                    refreshLayout.finishRefresh()
                    refreshLayout.resetNoMoreData()
                    refreshCallback.onRefresh?.invoke()
                }, 200)
            }
            smartRefreshLayout.setOnLoadMoreListener { refreshLayout ->
                CommonApp.globalHandler.postDelayed({
                    refreshLayout.finishLoadMore()
                    refreshCallback.onLoadMore?.invoke()
                }, 200)
            }
            return RefreshUtils(smartRefreshLayout)
        }
    }

    /**
     * @param enableLoadMore 是否开启加上拉加载功能（默认false-智能开启）
     */
    fun setEnableLoadMore(enableLoadMore: Boolean): RefreshUtils {
        smartRefreshLayout.setEnableLoadMore(enableLoadMore)
        return this
    }

    /**
     * @param enableRefresh 是否开启下拉刷新功能（默认true）
     */
    fun setEnableRefresh(enableRefresh: Boolean): RefreshUtils {
        smartRefreshLayout.setEnableRefresh(enableRefresh)
        return this
    }

    class RefreshCallback {
        internal var onRefresh: (() -> Unit)? = null
        internal var onLoadMore: (() -> Unit)? = null

        fun onRefresh(onRefresh: () -> Unit) {
            this.onRefresh = onRefresh
        }

        fun onLoadMore(onLoadMore: () -> Unit) {
            this.onLoadMore = onLoadMore
        }
    }
}